package kiwi.assist.dev.guieditor.view {
	import flash.system.Capabilities;

	import org.aswing.JScrollPane;
	import org.aswing.ASColor;
	import org.aswing.AssetIcon;
	import org.aswing.JButton;
	import org.aswing.JCheckBox;
	import org.aswing.JComboBox;
	import org.aswing.JLabel;
	import org.aswing.JPanel;
	import org.aswing.JSlider;
	import org.aswing.JTabbedPane;
	import org.aswing.JTree;
	import org.aswing.JWindow;
	import org.aswing.SoftBoxLayout;
	import org.aswing.SolidBackground;
	import org.aswing.border.TitledBorder;
	import org.aswing.colorchooser.VerticalLayout;

	import flash.display.Bitmap;
	import flash.display.Sprite;

	/**
	 * 编辑器的主界面
	 * @author zhangming.luo
	 */
	public class GuiEditorView extends Sprite {
		private var window : JWindow;
		private var tabpane : JTabbedPane;
		private var leftPanel : JPanel;
		private var centerPanel : JPanel;
		public var uiList : UiList;
		public var styleCombo : JComboBox;
		/**
		 * ui容器(ui的基类)的画布
		 */
		public var containerCanvas : CanvasView;
		/**
		 * ui的背景(都是DisplayObject或者是Bitmap,一般都是不响应鼠标事件的部分)
		 */
		public var backgroundCanvas : CanvasView;
		/**
		 * ui的内容(UiComponent的组件)
		 */
		public var contentCanvas : CanvasView;
		public var propertyList : PropertyTable;
		public var backgroundLayerCheckbox : JCheckBox;
		public var componentLayerCheckbox : JCheckBox;
		public var backgroundLayerLockCheckbox : JCheckBox;
		public var componentLayerLockCheckbox : JCheckBox;
		private var rightPanel : JPanel;
		public var skinTree : JTree;
		public var skinCanvas : SkinCanvasView;
		public var scaleSlider : JSlider;
		public var addBackgroundButton : JButton;
		private var framePanel : JPanel;
		private var menuBar : GuiEditorMenu;
		private var canvasPanel : JPanel;

		public function GuiEditorView() {
			window = new JWindow(this);
			window.x = 0;
			window.y = 0;
			window.setBackgroundDecorator(new SolidBackground(ASColor.LIGHT_GRAY));
			window.setSizeWH(1700, 1000);
			window.show();
			window.setContentPane(framePanel = new JPanel());
			framePanel.appendAll(menuBar = new GuiEditorMenu(), tabpane = new JTabbedPane());
			var panel : JPanel = new JPanel();
			panel.name = "GUI编辑器";
			panel.setLayout(new SoftBoxLayout(SoftBoxLayout.Y_AXIS));
			tabpane.append(panel);
			// 菜单部分
			// 内容部分
			var contentPanel : JPanel = new JPanel();
			contentPanel.setLayout(new SoftBoxLayout());
			// 左侧的面板
			leftPanel = new JPanel();
			leftPanel.setPreferredWidth(150);
			leftPanel.setLayout(new SoftBoxLayout(SoftBoxLayout.Y_AXIS));
			leftPanel.setBorder(new TitledBorder(null, "ui设置"));
			// 中间的面板
			centerPanel = new JPanel();
			centerPanel.setLayout(new SoftBoxLayout(SoftBoxLayout.Y_AXIS));
			canvasPanel = new JPanel();
			var canvasScrollPane : JScrollPane = new JScrollPane();
			canvasScrollPane.append(canvasPanel);
			canvasScrollPane.setPreferredHeight(800);
			centerPanel.append(canvasScrollPane);
			centerPanel.append(new ScrollControllerPanel(canvasPanel));
			// 右侧面板
			rightPanel = new JPanel();
			rightPanel.setLayout(new SoftBoxLayout(SoftBoxLayout.Y_AXIS));
			addStyleList();
			addLayerList();
			addBackgroundEditorPanel();
			addUiList();
			addPropertyList();
			initCanvas();
			addSkinMgr();
			panel.appendAll(contentPanel);
			contentPanel.appendAll(leftPanel, centerPanel, rightPanel);
		}

		private function addBackgroundEditorPanel() : void {
			leftPanel.append(addBackgroundButton = new JButton("添加一个背景"));
		}

		private function addSkinMgr() : void {
			// TODO: 增加对独立样式文件的支持
			skinTree = new JTree();
			skinTree.setPreferredWidth(100);
			// 皮肤画板
			var canvasPanel : JPanel = new JPanel();
			canvasPanel.setPreferredWidth(280);
			canvasPanel.setPreferredHeight(280);
			canvasPanel.setBackgroundDecorator(new SolidBackground(ASColor.WHITE));
			skinCanvas = new SkinCanvasView();
			canvasPanel.addChild(skinCanvas);
			// 皮肤的显示控制
			rightPanel.appendAll(skinTree, canvasPanel);
		}

		private function initCanvas() : void {
			canvasPanel.setBackgroundDecorator(new SolidBackground(ASColor.DARK_GRAY));
			canvasPanel.setPreferredWidth(1250);
			canvasPanel.setPreferredHeight(1200);
			containerCanvas = new CanvasView();
			canvasPanel.addChild(containerCanvas);
			backgroundCanvas = new CanvasView();
			canvasPanel.addChild(backgroundCanvas);
			contentCanvas = new CanvasView();
			canvasPanel.addChild(contentCanvas);
		}

		private function addUiList() : void {
			uiList = new UiList();
			uiList.setPreferredHeight(250);
			leftPanel.append(uiList);
		}

		private function addPropertyList() : void {
			var pane : JScrollPane = new JScrollPane();
			propertyList = new PropertyTable();
			pane.setPreferredHeight(Capabilities.screenResolutionY < 1080 ? 100 : 450);
			pane.append(propertyList);
			leftPanel.append(pane);
		}

		private function addStyleList() : void {
			var panel : JPanel = new JPanel();
			panel.setPreferredHeight(30);
			var label : JLabel = new JLabel("默认样式");
			styleCombo = new JComboBox();
			styleCombo.setPreferredWidth(80);
			panel.appendAll(label, styleCombo);
			leftPanel.append(panel);
		}

		private function addLayerList() : void {
			var panel : JPanel = new JPanel();
			panel.setLayout(new VerticalLayout());
			var panelBg : JPanel = new JPanel();
			panelBg.append(backgroundLayerCheckbox = new JCheckBox());
			panelBg.append(new JButton(null, new AssetIcon(new Bitmap(new VISIBLE))));
			panelBg.append(backgroundLayerLockCheckbox = new JCheckBox());
			panelBg.append(new JButton(null, new AssetIcon(new Bitmap(new LOCK))));
			panelBg.append(new JLabel("背景"));
			var panelCtrl : JPanel = new JPanel();
			panelCtrl.append(componentLayerCheckbox = new JCheckBox());
			panelCtrl.append(new JButton(null, new AssetIcon(new Bitmap(new VISIBLE))));
			panelCtrl.append(componentLayerLockCheckbox = new JCheckBox());
			panelCtrl.append(new JButton(null, new AssetIcon(new Bitmap(new LOCK))));
			panelCtrl.append(new JLabel("控件"));
			panel.appendAll(panelBg, panelCtrl);
			leftPanel.append(panel);
		}
	}
}
